// PTB2.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include "HotKeyPro.h"
#include "MyButton.h"
#include "MyLabel.h"
#include "MyTextbox.h"
#include "MyComboBox.h"
#include <commctrl.h>
#include "shellapi.h"
#include "commdlg.h"
#include "Vfw.h"
#include <MMSystem.h>
#include <Winioctl.h>
#include <Winuser.h>
#include <Windows.h>

//#include "shellapi.h"
///#include "commdlg.h"

#define  MY_WM_NOTIFYICON  WM_USER + 1

#define IMPORT __declspec(dllimport)
IMPORT BOOL SetHook(HINSTANCE);
IMPORT void RemoveHook();

IMPORT LRESULT CALLBACK KeyToolProc(int nCode,WPARAM wParam,LPARAM lParam);

//IMPORT void CALLBACK setToolState(int);		//DUNG DE SET TRANG THAI CUNG CHG TRINH

IMPORT BOOL CALLBACK AddHotKey(int QKey_1, int QKey_2, int QKey_3,TCHAR* szAppPath, int Flag);		// them 1 record cho quick app
IMPORT void CALLBACK RemoveQuickApp(int x);	
		BOOL IsQuickAppExists(TCHAR QKey_1, TCHAR QKey_2, TCHAR QKey_3);

IMPORT DllData CALLBACK GetDataInfo();
 
IMPORT void SetDllData(DllData &data);

IMPORT bool OpenCloseTray(bool bOpen);

IMPORT void LoadAppList(HWND hWnd);
IMPORT LPTSTR getValue(int x);

IMPORT int getCountApp();

IMPORT void AddKey(HWND handle);


IMPORT void SetUpDown( BOOL bState, int iUpDown );

IMPORT void OnTop(HWND hWnd, int &b);

HANDLE hToken; 
TOKEN_PRIVILEGES tkp;

int index = -1;
TCHAR szAppName[MAX_LOADSTRING];
LPTSTR str = new TCHAR[1000];
DllData data;
bool bOpenCD = 0;
int bOnTop = 0;

HWND hWnd;
HWND hListBox;

CMyButton btSolve;
CMyButton btEject;
CMyButton btOnTop;

CMyLabel lbPower1;
CMyLabel  lbOpen;

CMyTextBox txtGetKey;
CMyTextBox txtGetKeyOp;
CMyTextBox txtGetKeyOt;

CMyTextBox txtSolve;
CMyTextBox txtShowProgram;

MyComboBox cbBoxPower;
MyComboBox cbBoxSpecil;

MyComboBox cbBoxChooseKey1;
MyComboBox cbBoxChooseKey2;

MyComboBox cbBoxChooseKeyOp1;
MyComboBox cbBoxChooseKeyOp2;

MyComboBox cbBoxChooseKeyOt1;
MyComboBox cbBoxChooseKeyOt2;

// Global Variables:
HINSTANCE hInst;								// current instance
TCHAR szTitle[MAX_LOADSTRING];					// The title bar text
TCHAR szWindowClass[MAX_LOADSTRING];			// the main window class name

// Forward declarations of functions included in this code module:
ATOM				MyRegisterClass(HINSTANCE hInstance);
BOOL				InitInstance(HINSTANCE, int);
LRESULT CALLBACK	WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK	About(HWND, UINT, WPARAM, LPARAM);

void doMyNotify(HWND hFrameWnd, WPARAM wParam, LPARAM lParam);
BOOL MyTaskBarDeleteIcon(HWND hWnd, UINT uID) ;
void AppBrowse(HWND hWnd);
void getActualValue(int &value);

int APIENTRY _tWinMain(HINSTANCE hInstance,
	HINSTANCE hPrevInstance,
	LPTSTR    lpCmdLine,
	int       nCmdShow)
{
	UNREFERENCED_PARAMETER(hPrevInstance);
	UNREFERENCED_PARAMETER(lpCmdLine);
	
	// TODO: Place code here.
	MSG msg;
	HACCEL hAccelTable;

	// Initialize global strings
	LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
	LoadString(hInstance, IDC_HOTKEYPRO, szWindowClass, MAX_LOADSTRING);
	MyRegisterClass(hInstance);

	hInst = hInstance; // Store instance handle in our global variable
	SetHook(hInst);

	// Perform application initialization:
	if (!InitInstance (hInstance, nCmdShow))
	{
		return FALSE;
	}

	hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_HOTKEYPRO));

	// Main message loop:
	while (GetMessage(&msg, NULL, 0, 0))
	{
		if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
	}

	RemoveHook();
	return (int) msg.wParam;
}


ATOM MyRegisterClass(HINSTANCE hInstance)
{
	WNDCLASSEX wcex;

	wcex.cbSize = sizeof(WNDCLASSEX);

	wcex.style			= CS_HREDRAW | CS_VREDRAW;
	wcex.lpfnWndProc	= WndProc;
	wcex.cbClsExtra		= 0;
	wcex.cbWndExtra		= 0;
	wcex.hInstance		= hInstance;
	wcex.hIcon			= LoadIcon(hInstance, MAKEINTRESOURCE(IDI_HOTKEYPRO));
	wcex.hCursor		= LoadCursor(NULL, IDC_ARROW);
	wcex.hbrBackground	= (HBRUSH)(COLOR_WINDOW+1);
	wcex.lpszMenuName	= MAKEINTRESOURCE(IDI_HOTKEYPRO);
	wcex.lpszClassName	= szWindowClass;
	wcex.hIconSm		= LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));

	return RegisterClassEx(&wcex);
}


//   FUNCTION: InitInstance(HINSTANCE, int)
//
//   PURPOSE: Saves instance handle and creates main window
//
//   COMMENTS:
//
//        In this function, we save the instance handle in a global variable and
//        create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
	hWnd = CreateWindow(szWindowClass, szTitle, /*WS_OVERLAPPEDWINDOW*/WS_MINIMIZEBOX|WS_SYSMENU,
		350,10,535,700, NULL, NULL, hInstance, NULL);

	if (!hWnd)
	{
		return FALSE;
	}
	int token = 5;

	ShowWindow(hWnd, nCmdShow);
	UpdateWindow(hWnd);

	return TRUE;
}
void btnOK_OnClick()
{
}

BOOL MyTaskBarAddIcon(HWND hWnd, UINT uID, HICON hIcon, LPCWSTR lpszTip)
{ 
	
    BOOL Kq; 
    NOTIFYICONDATA tnid; 
 
    tnid.cbSize = sizeof(NOTIFYICONDATA); 
    tnid.hWnd = hWnd; 
    tnid.uID = uID; 
    tnid.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP; 
    tnid.uCallbackMessage = MY_WM_NOTIFYICON; 
    tnid.hIcon = hIcon;

    if (lpszTip) 
        lstrcpyn(tnid.szTip, lpszTip, sizeof(tnid.szTip)); 
    else 
        tnid.szTip[0] = '\0'; 
 
    Kq = Shell_NotifyIcon(NIM_ADD, &tnid); 
 
    if (hIcon) 
        DestroyIcon(hIcon);
 
    return Kq;  
} 

//  FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
//  PURPOSE:  Processes messages for the main window.
//
//  WM_COMMAND	- process the application menu
//  WM_PAINT	- Paint the main window
//  WM_DESTROY	- post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	int wmId, wmEvent;
	PAINTSTRUCT ps;
	HDC hdc;
	HICON hIcon;
	int value_1;
	LPCTSTR	str;
	int value_2;
	int value_3;
	int value_4;

	int selection;
	bool bHotKey;
	int nCountNull;

	switch (message)
	{

	case WM_CREATE:
		{	
			if (OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken)) 
			{
				LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, &tkp.Privileges[0].Luid); 
				tkp.PrivilegeCount = 1; 
				tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; 
				AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, (PTOKEN_PRIVILEGES)NULL, 0);
			}

			RECT rect;
			/////////////////---------BUTTON POWER-----------------/////////////////////

			btSolve.Create(_T("ShutDown"), WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON|BS_CENTER,120,25,80,40, hWnd, IDB_SHUTDOWN);

			btSolve.Create(_T("Restart"), WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON|BS_CENTER,200,25,80,40, hWnd, IDB_RESTART);

			btSolve.Create(_T("Log Off"), WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON|BS_CENTER,280,25,80,40, hWnd, IDB_LOGOFF);

			btSolve.Create(_T("Standby"), WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON|BS_CENTER,360,25,80,40, hWnd, IDB_STANDBY);

			btSolve.Create(_T("Hibernate"), WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON|BS_CENTER,440,25,80,40, hWnd, IDB_HIBERNATE);

			////////////////////////////////////////////////////////////////////////////////
			cbBoxPower.Create(NULL,WS_CHILD | WS_VISIBLE | WS_BORDER | CBS_SORT |CBS_DROPDOWNLIST |
                    WS_VSCROLL | WS_TABSTOP,120,80,80,40,hWnd,IDCB_POWER_1);

			SendMessage(cbBoxPower.GetHandle(),CB_ADDSTRING,0,(LPARAM)_T(""));
			SendMessage(cbBoxPower.GetHandle(),CB_ADDSTRING,0,(LPARAM)_T("ShutDown"));
			SendMessage(cbBoxPower.GetHandle(),CB_ADDSTRING,0,(LPARAM)_T("Restart"));
			SendMessage(cbBoxPower.GetHandle(),CB_ADDSTRING,0,(LPARAM)_T("Log Off"));
			SendMessage(cbBoxPower.GetHandle(),CB_ADDSTRING,0,(LPARAM)_T("Standby"));
			SendMessage(cbBoxPower.GetHandle(),CB_ADDSTRING,0,(LPARAM)_T("Hibernate"));
			//////////////////////////////////////////////////////////////////////////////////
			cbBoxChooseKey1.Create(NULL,WS_CHILD | WS_VISIBLE | WS_BORDER | CBS_SORT |CBS_DROPDOWNLIST |
                    WS_VSCROLL | WS_TABSTOP,200,80,80,40,hWnd,IDCB_POWER_2);

			SendMessage(cbBoxChooseKey1.GetHandle(),CB_ADDSTRING,0,(LPARAM)_T(""));
			SendMessage(cbBoxChooseKey1.GetHandle(),CB_ADDSTRING,0,(LPARAM)_T("CTRL"));
			SendMessage(cbBoxChooseKey1.GetHandle(),CB_ADDSTRING,0,(LPARAM)_T("ALT"));
			SendMessage(cbBoxChooseKey1.GetHandle(),CB_ADDSTRING,0,(LPARAM)_T("SHIFT"));
			SendMessage(cbBoxChooseKey1.GetHandle(),CB_ADDSTRING,0,(LPARAM)_T("WINDOW"));
			//////////////////////////////////////////////////////////////////////////////////
			cbBoxChooseKey2.Create(NULL,WS_CHILD | WS_VISIBLE | WS_BORDER | CBS_SORT |CBS_DROPDOWNLIST |
                    WS_VSCROLL | WS_TABSTOP,280,80,80,40,hWnd,IDCB_POWER_3);

			SendMessage(cbBoxChooseKey2.GetHandle(),CB_ADDSTRING,0,(LPARAM)_T(""));
			SendMessage(cbBoxChooseKey2.GetHandle(),CB_ADDSTRING,0,(LPARAM)_T("CTRL"));
			SendMessage(cbBoxChooseKey2.GetHandle(),CB_ADDSTRING,0,(LPARAM)_T("ALT"));
			SendMessage(cbBoxChooseKey2.GetHandle(),CB_ADDSTRING,0,(LPARAM)_T("SHIFT"));
			SendMessage(cbBoxChooseKey2.GetHandle(),CB_ADDSTRING,0,(LPARAM)_T("WINDOW"));
			//////////////////////////////////////////////////////////////////////////////////
			txtGetKey.Create(NULL, WS_BORDER | WS_CHILD | WS_VISIBLE|ES_AUTOHSCROLL|ES_CENTER | ES_UPPERCASE, 370,80,50,25, hWnd, IDT_GETKEYPOWER);

			btSolve.Create(_T("ADD"), WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON|BS_CENTER,450,80,60,25, hWnd, IDB_SAVEKEYPOWER);

			/////////////////////////////////////////////////////////////////////////////////////
			lbPower1.Create(_T("Power Function And Hot Key Power "), WS_BORDER | WS_CHILD | WS_VISIBLE | ES_AUTOHSCROLL|ES_CENTER, 10,25,100,80, hWnd, IDT_TEX);
			////////////////////////////////////////////////////////////////////////////////////////
			////////////////////////////////////////////////////////////////////////////////////////
			//////////////////////////////////////QUICK OPEN PROGRAMME//////////////////////////////
			lbPower1.Create(_T("Quick Open Application"), WS_BORDER | WS_CHILD | WS_VISIBLE | ES_AUTOHSCROLL|ES_CENTER, 10,150,100,80, hWnd, IDT_TEX);
			
			btSolve.Create(_T("Choose Application"), WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON|BS_CENTER,120,150,140,40, hWnd, IDB_CHOOSEPROGRAM);

			txtShowProgram.Create(NULL, WS_BORDER | WS_CHILD | WS_VISIBLE|ES_AUTOHSCROLL|ES_CENTER, 270,150,250,40, hWnd, IDT_GETKEYPOWER);
			//////////////////////////////////////////////////////////////////////////////////
			cbBoxChooseKeyOp1.Create(NULL,WS_CHILD | WS_VISIBLE | WS_BORDER | CBS_SORT |CBS_DROPDOWNLIST |
                    WS_VSCROLL | WS_TABSTOP,200,200,80,40,hWnd,IDCB_POWER_4);

			SendMessage(cbBoxChooseKeyOp1.GetHandle(),CB_ADDSTRING,0,(LPARAM)_T(""));
			SendMessage(cbBoxChooseKeyOp1.GetHandle(),CB_ADDSTRING,0,(LPARAM)_T("CTRL"));
			SendMessage(cbBoxChooseKeyOp1.GetHandle(),CB_ADDSTRING,0,(LPARAM)_T("ALT"));
			SendMessage(cbBoxChooseKeyOp1.GetHandle(),CB_ADDSTRING,0,(LPARAM)_T("SHIFT"));
			SendMessage(cbBoxChooseKeyOp1.GetHandle(),CB_ADDSTRING,0,(LPARAM)_T("WINDOW"));
			//////////////////////////////////////////////////////////////////////////////////
			cbBoxChooseKeyOp2.Create(NULL,WS_CHILD | WS_VISIBLE | WS_BORDER | CBS_SORT |CBS_DROPDOWNLIST |
                    WS_VSCROLL | WS_TABSTOP,280,200,80,40,hWnd,IDCB_POWER_5);

			SendMessage(cbBoxChooseKeyOp2.GetHandle(),CB_ADDSTRING,0,(LPARAM)_T(""));
			SendMessage(cbBoxChooseKeyOp2.GetHandle(),CB_ADDSTRING,0,(LPARAM)_T("CTRL"));
			SendMessage(cbBoxChooseKeyOp2.GetHandle(),CB_ADDSTRING,0,(LPARAM)_T("ALT"));
			SendMessage(cbBoxChooseKeyOp2.GetHandle(),CB_ADDSTRING,0,(LPARAM)_T("SHIFT"));
			SendMessage(cbBoxChooseKeyOp2.GetHandle(),CB_ADDSTRING,0,(LPARAM)_T("WINDOW"));
			//////////////////////////////////////////////////////////////////////////////////
			txtGetKeyOp.Create(NULL, WS_BORDER | WS_CHILD | WS_VISIBLE|ES_AUTOHSCROLL|ES_CENTER | ES_UPPERCASE, 370,200,50,25, hWnd, IDT_GETKEYPOWER);

			btSolve.Create(_T("ADD"), WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON|BS_CENTER,450,200,60,25, hWnd, IDB_SAVEQUICKAPP);

			lbPower1.Create(_T("Set Key"), WS_BORDER | WS_CHILD | WS_VISIBLE | ES_AUTOHSCROLL|ES_CENTER, 120,200,60,25, hWnd, IDT_TEX);

			///////////////////////////////////////////////////////////////////////////////////
			///////////////////////////////////////////////////////////////////////////////////
			///////////////////////////////List Of Hot Key/////////////////////////////////////
			lbPower1.Create(_T("List Of Hot Keys"), WS_BORDER | WS_CHILD | WS_VISIBLE | ES_AUTOHSCROLL|ES_CENTER, 10,240,100,80, hWnd, IDT_TEX);

			hListBox = CreateWindow(L"LISTBOX", NULL, WS_VISIBLE | WS_CHILD | WS_BORDER |  LBS_NOINTEGRALHEIGHT | WS_VSCROLL | WS_TABSTOP , 
				 121,240,320,150, hWnd,  (HMENU)IDT_GETKEYPOWER, hInst, NULL);

			btSolve.Create(_T("Remove"), WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON|BS_CENTER,450,300,70,40, hWnd, IDB_REMOVEKEYLIST);
			///////////////////////////////////////////////////////////////////////////////////
			///////////////////////////////////////////////////////////////////////////////////
			///////////////////////////////Specil Function/////////////////////////////////////

			lbPower1.Create(_T("Special Functions"), WS_BORDER | WS_CHILD | WS_VISIBLE | ES_AUTOHSCROLL|ES_CENTER, 10,410,100,80, hWnd, IDT_TEX);

			//Button
			btSolve.Create(_T("Vol Up"), WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON|BS_CENTER,120,410,80,40, hWnd, IDB_VOLUP);

			btSolve.Create(_T("Vol Down"), WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON|BS_CENTER,200,410,80,40, hWnd, IDB_VOLDOWN);

			btEject.Create(_T("Eject CD"), WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON|BS_CENTER,280,410,80,40, hWnd, IDB_EJECT);

			btOnTop.Create(_T("On Top"), WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON|BS_CENTER,360,410,80,40, hWnd, IDB_ONTOP);

			btSolve.Create(_T("Screen Off"), WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON|BS_CENTER,440,410,80,40, hWnd, IDB_SCREENOF);


			cbBoxChooseKeyOt1.Create(NULL,WS_CHILD | WS_VISIBLE | WS_BORDER | CBS_SORT |CBS_DROPDOWNLIST |
                    WS_VSCROLL | WS_TABSTOP,200,460,80,40,hWnd,IDCB_POWER_6);

			SendMessage(cbBoxChooseKeyOt1.GetHandle(),CB_ADDSTRING,0,(LPARAM)_T(""));
			SendMessage(cbBoxChooseKeyOt1.GetHandle(),CB_ADDSTRING,0,(LPARAM)_T("CTRL"));
			SendMessage(cbBoxChooseKeyOt1.GetHandle(),CB_ADDSTRING,0,(LPARAM)_T("ALT"));
			SendMessage(cbBoxChooseKeyOt1.GetHandle(),CB_ADDSTRING,0,(LPARAM)_T("SHIFT"));
			SendMessage(cbBoxChooseKeyOt1.GetHandle(),CB_ADDSTRING,0,(LPARAM)_T("WINDOW"));
			//////////////////////////////////////////////////////////////////////////////////
			cbBoxChooseKeyOt2.Create(NULL,WS_CHILD | WS_VISIBLE | WS_BORDER | CBS_SORT |CBS_DROPDOWNLIST |
                    WS_VSCROLL | WS_TABSTOP,280,460,80,40,hWnd,IDCB_POWER_7);

			SendMessage(cbBoxChooseKeyOt2.GetHandle(),CB_ADDSTRING,0,(LPARAM)_T(""));
			SendMessage(cbBoxChooseKeyOt2.GetHandle(),CB_ADDSTRING,0,(LPARAM)_T("CTRL"));
			SendMessage(cbBoxChooseKeyOt2.GetHandle(),CB_ADDSTRING,0,(LPARAM)_T("ALT"));
			SendMessage(cbBoxChooseKeyOt2.GetHandle(),CB_ADDSTRING,0,(LPARAM)_T("SHIFT"));
			SendMessage(cbBoxChooseKeyOt2.GetHandle(),CB_ADDSTRING,0,(LPARAM)_T("WINDOW"));
			//////////////////////////////////////////////////////////////////////////////////
			txtGetKeyOt.Create(NULL, WS_BORDER | WS_CHILD | WS_VISIBLE|ES_AUTOHSCROLL|ES_CENTER | ES_UPPERCASE, 370,460,50,25, hWnd, IDT_GETKEYPOWER);

			btSolve.Create(_T("ADD"), WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON|BS_CENTER,450,460,60,25, hWnd, IDB_SAVEKEY_1);

			cbBoxSpecil.Create(NULL,WS_CHILD | WS_VISIBLE | WS_BORDER /*| CBS_DROPDOWN*/ | CBS_SORT |CBS_DROPDOWNLIST |
                    WS_VSCROLL | WS_TABSTOP,120,460,80,40,hWnd,IDCB_POWER_8);

			SendMessage(cbBoxSpecil.GetHandle(),CB_ADDSTRING,0,(LPARAM)_T(""));
			SendMessage(cbBoxSpecil.GetHandle(),CB_ADDSTRING,0,(LPARAM)_T("Vol Up"));
			SendMessage(cbBoxSpecil.GetHandle(),CB_ADDSTRING,0,(LPARAM)_T("Vol Down"));
			SendMessage(cbBoxSpecil.GetHandle(),CB_ADDSTRING,0,(LPARAM)_T("Eject CD"));
			//SendMessage(cbBoxSpecil.GetHandle(),CB_ADDSTRING,0,(LPARAM)_T("Close CD"));
			SendMessage(cbBoxSpecil.GetHandle(),CB_ADDSTRING,0,(LPARAM)_T("On Top"));
			//SendMessage(cbBoxSpecil.GetHandle(),CB_ADDSTRING,0,(LPARAM)_T("Off Top"));
			SendMessage(cbBoxSpecil.GetHandle(),CB_ADDSTRING,0,(LPARAM)_T("Screen Off"));
			//SendMessage(cbBoxSpecil.GetHandle(),CB_ADDSTRING,0,(LPARAM)_T("On Tray"));
			//SendMessage(cbBoxSpecil.GetHandle(),CB_ADDSTRING,0,(LPARAM)_T("Off Tray"));
			//SendMessage(cbBoxSpecil.GetHandle(),CB_ADDSTRING,0,(LPARAM)_T("Close"));

			////////////////////////////////////////////////////////////////////////////////////
			///////////////////////////////////////////////////////////////////////////////////
			///////////////////////////////////////////////////////////////////////////////////
			btSolve.Create(_T("System Tray"), WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON|BS_CENTER,300,500,100,25, hWnd, IDB_SYSTEMSTRAY);

			btSolve.Create(_T("Close"), WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON|BS_CENTER,410,500,100,25, hWnd, IDM_EXIT);
			LoadAppList(hListBox);
		};
		break;
	case WM_COMMAND:
		wmId    = LOWORD(wParam);
		wmEvent = HIWORD(wParam);
		


	/*switch(wmEvent) // bat su kien khi chon tu combobox
	{
		
	case CBN_SELENDOK:
		{
		char msgText[256];
		
		index = (int)SendMessage(cbBoxPower.GetHandle(), CB_GETCURSEL, 0, 0);
		if(index == 0 )
			MessageBox(hWnd,_T("None"),_T("Warning"), MB_OK);
		else if(index == 1 )
			MessageBox(hWnd,_T("Hibernate"),_T("Warning"), MB_OK);
		}		
		break;
	}*/




		switch (wmId)
		{
		////////////////////////////////////////////////////////////////////////////////////////////
		////////////////////////////////////////////////////////////////////////////////////////////
		///////////////////////////////////POWER BUTTON//////////////////////////////////
		case IDB_SHUTDOWN:
			{
				if (wmEvent == BN_CLICKED)
				{
					if( MessageBox(hWnd,_T("Are you sure to Shutdown?"),_T("Warning"), MB_YESNO) == IDYES)
					{
						 ExitWindowsEx(EWX_SHUTDOWN  | EWX_POWEROFF |EWX_FORCE,0);
					}
				}
			}
			break;

		case IDB_RESTART:
		{
			if (wmEvent == BN_CLICKED)
			{
				if( MessageBox(hWnd,_T("Are you sure to Restart?"),_T("Warning"), MB_YESNO) == IDYES)
						ExitWindowsEx(EWX_REBOOT | EWX_FORCE,0);
			}
		}
		break;

		case IDB_LOGOFF:
		{
			if (wmEvent == BN_CLICKED)
			{
				if( MessageBox(hWnd,_T("Are you sure to LogOff?"),_T("Warning"), MB_YESNO) == IDYES)
					ExitWindowsEx(EWX_LOGOFF,SHTDN_REASON_FLAG_PLANNED);
			}
		}
		break;

		case IDB_STANDBY:
		{
			if (wmEvent == BN_CLICKED)
			{
				if( MessageBox(hWnd,_T("Are you sure to StandBy?"),_T("Warning"), MB_YESNO) == IDYES)
					SetSystemPowerState(TRUE,TRUE);
			}
		}
		break;

		case IDB_HIBERNATE:
		{
			if (wmEvent == BN_CLICKED)
			{
				if( MessageBox(hWnd,_T("Are you sure to Shutdown?"),_T("Warning"), MB_YESNO) == IDYES)
					SetSystemPowerState(FALSE,FALSE);
			}
		}
		break;
		case IDB_REMOVEKEYLIST:
			selection = SendMessage(hListBox,LB_GETCURSEL,NULL,NULL);
			SendMessage(hListBox,LB_DELETESTRING,(WPARAM)selection,NULL);

			RemoveQuickApp(selection);
			 break;
		case IDB_SAVEKEYPOWER:
			value_4 = (int)SendMessage(cbBoxPower.GetHandle(), CB_GETCURSEL, 0, 0);
			
			switch (value_4)
			{
			case 0:
				value_4 = -1;
				break;
			case 1:
				value_4 = ID_HIBERNATE;
				break;
			case 2:
				value_4 = ID_LOGOFF;
				break;
			case 3:
				value_4 = ID_RESTART;
				break;
			case 4:
				value_4 = ID_SHUTDOWN;
				break;
			case 5:
				value_4 = ID_STANDBY;
				break;
			}

			value_1 = (int)SendMessage(cbBoxChooseKey1.GetHandle(), CB_GETCURSEL, 0, 0);
			
			getActualValue(value_1);

			value_2 = (int)SendMessage(cbBoxChooseKey2.GetHandle(), CB_GETCURSEL, 0, 0);

			getActualValue(value_2);

			str = txtGetKey.GetText();
			value_3 = str[0];

			nCountNull = 0;

			if (value_1 == -1)
				++nCountNull;

			if (value_2 == -1)
				++nCountNull;

			if (value_3 == 0)
				++nCountNull;

			if (value_4 == -1)
				MessageBox(hWnd, _T("Ban phai chon chuc nang"), _T("Thong Bao"), NULL);
			else
				if (nCountNull > 1)
					MessageBox(hWnd, _T("Phim tat phai co it nhat 2 phim"), _T("Thong Bao"), NULL);
				else
				{
					bHotKey = AddHotKey(value_1, value_2, value_3, _T(""), value_4);
					if (bHotKey)
						AddKey(hListBox);
				}
			
			break;
		case IDB_SAVEQUICKAPP:
			value_1 = (int)SendMessage(cbBoxChooseKeyOp1.GetHandle(), CB_GETCURSEL, 0, 0);
			
			getActualValue(value_1);

			value_2 = (int)SendMessage(cbBoxChooseKeyOp2.GetHandle(), CB_GETCURSEL, 0, 0);

			getActualValue(value_2);

			str = txtGetKeyOp.GetText();
			value_3 = str[0];

			nCountNull = 0;

			if (value_1 == -1)
				++nCountNull;

			if (value_2 == -1)
				++nCountNull;

			if (value_3 == 0)
				++nCountNull;
			else
				if (nCountNull > 1)
					MessageBox(hWnd, _T("Phim tat phai co it nhat 2 phim"), _T("Thong Bao"), NULL);
				else
				{
					bHotKey = AddHotKey(value_1, value_2, value_3, szAppName, ID_APP);
					if (bHotKey)
						AddKey(hListBox);
				}
			break;
		case IDB_SAVEKEY_1:
			value_4 = (int)SendMessage(cbBoxSpecil.GetHandle(), CB_GETCURSEL, 0, 0);
			
			switch (value_4)
			{
			case 0:
				value_4 = -1;
				break;
			case 1:
				value_4 = ID_EJECTCD;
				break;
			case 2:
				value_4 = ID_ONTOP;
				break;
			case 3:
				value_4 = ID_SCREENOFF;
				break;
			case 4:
				value_4 = ID_VOLDOWN;
				break;
			case 5:
				value_4 = ID_VOLUP;
				break;
			}

			value_1 = (int)SendMessage(cbBoxChooseKeyOt1.GetHandle(), CB_GETCURSEL, 0, 0);
			
			getActualValue(value_1);

			value_2 = (int)SendMessage(cbBoxChooseKeyOt2.GetHandle(), CB_GETCURSEL, 0, 0);

			getActualValue(value_2);

			str = txtGetKeyOt.GetText();
			value_3 = str[0];
			
			nCountNull = 0;

			if (value_1 == -1)
				++nCountNull;

			if (value_2 == -1)
				++nCountNull;

			if (value_3 == 0)
				++nCountNull;

			if (value_4 == -1)
				MessageBox(hWnd, _T("Ban phai chon chuc nang"), _T("Thong Bao"), NULL);
			else
				if (nCountNull > 1)
					MessageBox(hWnd, _T("Phim tat phai co it nhat 2 phim"), _T("Thong Bao"), NULL);
				else
				{
					bHotKey = AddHotKey(value_1, value_2, value_3, _T(""), value_4);
					if (bHotKey)
						AddKey(hListBox);
				}
			break;
		////////////////////////////////////////////////////////////////////////////////////////////////
		////////////////////////////////////////////////////////////////////////////////////////////////
		////////////////////////////////5//OTHER FUNCTION///////////////////////////////////////////////
		case IDB_CHOOSEPROGRAM:
				AppBrowse(hWnd);
				break;
		case IDB_SYSTEMSTRAY:
			ShowWindow(hWnd, SW_HIDE);
			hIcon = LoadIcon(hInst, MAKEINTRESOURCE(IDI_ICON1));
			MyTaskBarAddIcon(hWnd, TRAY_ICON_ID, hIcon,szTitle);
			break;
		case IDB_VOLUP:
			for (int i = 0; i < 6; i++)
				SetUpDown( TRUE,VK_VOLUME_UP );
			break;
		case IDB_VOLDOWN:
			for (int i = 0; i < 6; i++)
				SetUpDown( TRUE, VK_VOLUME_DOWN );
			break;
		case IDB_EJECT:
			bOpenCD = !bOpenCD;	
			OpenCloseTray(bOpenCD);

			if (bOpenCD)
				btEject.SetText(_T("Close CD"));
			else
				btEject.SetText(_T("Eject CD"));
			break;
		case IDB_ONTOP:
			OnTop(hWnd, bOnTop);

			if (bOnTop)
				btOnTop.SetText(_T("Off On Top"));
			else
				btOnTop.SetText(_T("On Top"));
			break;
		case IDB_SCREENOF:
			SendMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, (LPARAM) 2);
			break;
		case IDM_ABOUT:
			DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
			break;
		case IDM_EXIT:
			DestroyWindow(hWnd);
			break;
		default:
			return DefWindowProc(hWnd, message, wParam, lParam);
		break;
		}
	
	case WM_PAINT:
		hdc = BeginPaint(hWnd, &ps);
		// TODO: Add any drawing code here...
		EndPaint(hWnd, &ps);
		break;
	case MY_WM_NOTIFYICON:
		doMyNotify(hWnd, wParam, lParam);
		break;
	//case WM_USER:
	//	{
	//	KeyToolProc(1, wParam, lParam);
	//	}
	//	break;
	case WM_DESTROY:
		PostQuitMessage(0);
		break;
	default:
		return DefWindowProc(hWnd, message, wParam, lParam);
	}
	return 0;
}

// Message handler for about box.
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
	UNREFERENCED_PARAMETER(lParam);
	switch (message)
	{
	case WM_INITDIALOG:
		return (INT_PTR)TRUE;

	case WM_COMMAND:
		if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
		{
			EndDialog(hDlg, LOWORD(wParam));
			return (INT_PTR)TRUE;
		}
		break;
	}
	return (INT_PTR)FALSE;
}

BOOL MyTaskBarDeleteIcon(HWND hWnd, UINT uID) 
{ 
    BOOL Kq; 
    NOTIFYICONDATA tnid; 
 
    tnid.cbSize = sizeof(NOTIFYICONDATA); 
    tnid.hWnd = hWnd; 
    tnid.uID = uID; 
         
    Kq = Shell_NotifyIcon(NIM_DELETE, &tnid); 
    return Kq; 
} 

void doMyNotify(HWND hFrameWnd, WPARAM wParam, LPARAM lParam)
{
	if (lParam==WM_LBUTTONDOWN) {

		MyTaskBarDeleteIcon(hFrameWnd, TRAY_ICON_ID);
		
		ShowWindow(hFrameWnd, SW_SHOW);

		ShowWindow(hFrameWnd, SW_RESTORE);

	}
}
//
//bool OpenCloseTray(bool bOpen)
//{
//    // Open the device (drive) that we want to affect
//	DWORD nSystemDrives = GetLogicalDrives();
//	TCHAR szDriveRoot[] = _T("x:\\");
//	TCHAR cDrive;
//	TCHAR* cs = new TCHAR[255];
//
//	for(cDrive = 'A'; cDrive <= 'Z'; cDrive++, nSystemDrives >>= 1)
//	{
//        if(!(nSystemDrives & 1))
//            continue;
//	
//		szDriveRoot[0] = cDrive;
//		UINT nDriveType = GetDriveType(szDriveRoot);
//	
//		if(nDriveType == DRIVE_CDROM)
//		{
//			swprintf(cs, _T("\\\\.\\%c:"), cDrive);
//			break;
//		}
//	}
//	//MessageBox(hWnd, cs, _T("Thong Bao"), 0);
//    //cs.Format(_T("\\\\.\\%c:"),cDrive);
//    HANDLE hDrive = CreateFile(cs, GENERIC_READ, FILE_SHARE_READ, 
//                               NULL, OPEN_EXISTING, 
//    FILE_ATTRIBUTE_NORMAL, NULL);
//
//    // Make sure the device was found and opened successfully
//    if(hDrive == INVALID_HANDLE_VALUE || GetLastError() != NO_ERROR)
//        return false;
//
//    BOOL bStatus; // Let the caller know if it worked or not
//    DWORD dwDummy; // We don't really need this info
//    if(bOpen) // Open the tray
//        bStatus = DeviceIoControl(hDrive, IOCTL_STORAGE_EJECT_MEDIA, NULL, 
//                                  0, NULL, 0, &dwDummy, NULL);
//    else // Close the tray
//        bStatus = DeviceIoControl(hDrive, IOCTL_STORAGE_LOAD_MEDIA, 
//                                  NULL, 0, NULL, 0, &dwDummy, NULL);
//    CloseHandle(hDrive);
//    return bStatus?true:false;
//}

void AppBrowse(HWND hWnd)
{
	OPENFILENAME ofn;
	ZeroMemory(&ofn,sizeof(OPENFILENAME));	
	ofn.lStructSize = sizeof(OPENFILENAME);
	ofn.hwndOwner = hWnd;
	ofn.lpstrFilter = L"*.EXE\0*.EXE\0" ;
	ofn.nFilterIndex = 1;
	ofn.lpstrFile = szAppName;
	ofn.lpstrFile[0] = '\0';
	ofn.nMaxFile = sizeof(szAppName);
	ofn.lpstrFileTitle = NULL;
	ofn.nMaxFileTitle = 0;
	ofn.lpstrInitialDir = NULL;
	ofn.Flags = OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST;

	if (GetOpenFileName(&ofn)){
		SendMessage(txtShowProgram.hWnd,WM_SETTEXT,NULL,(LPARAM)szAppName);
	}
}

void getActualValue(int &value)
{
		switch (value)
		{
		case 0:
			value = -1;
			break;
		case 1:
			value = VK_MENU;
			break;
		case 2:
			value = VK_CONTROL;
			break;
		case 3:
			value = VK_SHIFT;
			break;
		case 4:
			value = VK_LWIN;
			break;
		}
}
